package ua.epam.muzalevskiy.traction.commands;

import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import ua.epam.muzalevskiy.traction.configuretion.MessageConfigurer;
import ua.epam.muzalevskiy.traction.configuretion.PagePathConfigurer;

/**
 * <code>ViewAdministrationPageCommand</code> class is the <code>Command</code> 
 * interface implementation, that realized <code>Command</code> pattern.
 * Current class encapsulate action of administration page displaying, only
 * users that logged as administrator can be forward to this page.
 *
 * @author Muzalevskiy Vitaliy
 * @see Command
 */
public class ViewAdministrationPageCommand implements Command {
    
    private static final String SESSION_ATTRIBUTE_NAME_USER_TYPE = "user_type";

    @Override
    public String execute(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        String page;
        if ("ADMIN".equals(request.getSession().
                getAttribute(SESSION_ATTRIBUTE_NAME_USER_TYPE))) {
            page = PagePathConfigurer.getInstance()
                    .getProperty(PagePathConfigurer.ADMINISTRATION_PAGE);
        } else {
            request.setAttribute(Command.ATTRIBUTE_NAME_ERROR_MESSAGE, 
                    MessageConfigurer.getInstance()
                    .getProperty(MessageConfigurer.NOT_ADMINISTRATOR_ERROR_MESSAGE));
            page = PagePathConfigurer.getInstance()
                    .getProperty(PagePathConfigurer.ERROR_PAGE);
        }
        return page;
    }
}
